define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'crm/buser/index',
                    add_url: 'crm/buser/add',
                    edit_url: 'crm/buser/edit',
                    del_url: 'crm/buser/del',
                    multi_url: 'crm/buser/multi',
                    table: 'buser',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'username', title: __('Username'), operate: 'like'},
                        {field: 'qq', title: __('Qq'), operate: 'like'},
                        {field: 'wechat', title: __('Wechat'), operate: 'like'},
                        {field: 'mobile', title: __('Mobile'), operate: 'like'},
                        {field: 'email', title: __('Email'), operate: 'like'},
                        {field: 'gender', title: __('Gender'), searchList: {"1":__('Gender 1'),"2":__('Gender 2')}, formatter: Table.api.formatter.normal},
                        {field: 'birthday', title: __('Birthday'), operate:'RANGE', addclass:'datetimerange'},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'qq_friend', title: __('Qq_friend'), searchList: {"1":__('Qq_friend 1'),"2":__('Qq_friend 2')}, formatter: Table.api.formatter.normal},
                        {field: 'wecaht_friend', title: __('Wecaht_friend'), searchList: {"1":__('Wecaht_friend 1'),"2":__('Wecaht_friend 2')}, formatter: Table.api.formatter.normal},
                        {field: 'typelist', title: __('Typelist'),searchList: {"1":__('Typelist 1'),"2":__('Typelist 2')},formatter: Table.api.formatter.normal},
                        {field: 'station', title: __('Station'),operate:'LIKE'},
                        {field: 'company.company', title: __('Company.company'), operate: 'like'},

                        {field: 'operate', title: __('Operate'), table: table,
                            events: Table.api.events.operate,
                            formatter: function (value, row, index) {
                                var that = $.extend({}, this);
                                var table = $(that.table).clone(true);
                                $(table).data("operate-del", null);
                                that.table = table;
                                return Table.api.formatter.operate.call(that, value, row, index);
                            }
                          /*  buttons: [
                                {
                                    name: '分配客服',
                                    text: __('分配客服'),
                                    title: __('分配客服'),
                                    classname: 'btn btn-xs btn-primary btn-dialog',
                                    icon: 'fa fa-list',
                                    url: 'crm/allot/push_products',
                                    callback: function (data) {
                                        Layer.alert("接收到回传数据：" + JSON.stringify(data), {title: "回传数据"});
                                    },
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        return true;
                                    }
                                }
                                ]*/
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});